<?php

class CategoryController extends Controller {

    public function indexAction() {
        $catgoryModel = Factory::M("CategoryModel");
        $categories = $catgoryModel->getCategories();
        include CURRENT_VIEW_PATH . 'cat_list.html';
    }

    public function addAction() {
        $categoryModel = Factory::M("CategoryModel");
        $categories = $categoryModel->getCategories();
        include CURRENT_VIEW_PATH . 'cat_add.html';
    }

    public function insertAction() {
        //1.收集表单数据
        $data = array();
        $data['cat_name'] = trim($_POST['cat_name']);
        $data['parent_id'] = $_POST['parent_id'];
        $data['unit'] = trim($_POST['unit']);
        $data['sort_order'] = trim($_POST['sort_order']);
        $data['is_show'] = $_POST['is_show'];
        $data['cat_desc'] = trim($_POST['cat_desc']);

        //2.验证及处理
        if($data['cat_name'] === "") {
            $this->_jump("index.php?p=back&c=category&a=add", "分类名称不能为空");
        }

        $categoryModel = Factory::M("CategoryModel");
        if($categoryModel->insert($data)) {
            $this->_jump("index.php?p=back&c=category&a=index", "添加分类成功", 2);
        }
        else {
            $this->_jump("index.php?p=back&c=category&a=add", "添加分类失败");
        }
    }

    public function editAction() {
        $cat_id = $_GET['cat_id'] + 0;
        $categoryModel = Factory::M("CategoryModel");
        $category = $categoryModel->selectByPk($cat_id);
        $categories = $categoryModel->getCategories();
        include CURRENT_VIEW_PATH . 'cat_edit.html';
    }

    public function updateAction() {
        $data = array();
        $data['cat_name'] = trim($_POST['cat_name']);
        $data['parent_id'] = $_POST['parent_id'];
        $data['unit'] = trim($_POST['unit']);
        $data['sort_order'] = trim($_POST['sort_order']);
        $data['is_show'] = $_POST['is_show'];
        $data['cat_desc'] = trim($_POST['cat_desc']);
        $data['cat_id'] = $_POST['cat_id'];

        if($data['cat_name'] === "") {
            $this->_jump("index.php?p=back&c=category&a=edit&cat_id=".$data['cat_id'], "分类名称不能为空");
        }

        $categoryModel = Factory::M("CategoryModel");

        $ids = $categoryModel->getSubIds($data['cat_id']);
        $ids[] = $data['cat_id'];
        if(in_array($data['parent_id'], $ids)) {
            $this->_jump("index.php?p=back&c=category&a=edit&cat_id=".$data['cat_id'], "不能将当前节点及其子节点作为上级节点");
        }

        if($categoryModel->update($data)) {
            $this->_jump("index.php?p=back&c=category&a=index", "更新分类成功", 1);
        }
        else {
            $this->_jump("index.php?p=back&c=category&a=edit&cat_id=".$data['cat_id'], "添加分类失败");
        }
    }

    public function deleteAction() {
        $cat_id = $_GET['cat_id'] + 0;

        $categoryModel = Factory::M("CategoryModel");

        $ids = $categoryModel->getSubIds($cat_id);
        if(!empty($ids)) {
            $this->_jump("index.php?p=back&c=category&a=index", "当前分类最下层分类，删除失败", 4);
        }

        if($categoryModel->delete($cat_id)) {
            $this->_jump("index.php?p=back&c=category&a=index", "删除分类成功", 1);
        }
        else {
            $this->_jump("index.php?p=back&c=category&a=index", "删除分类失败");
        }
    }

}